package jp.millibit.util
{
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;

	use namespace flash_proxy;

	public dynamic class Embed extends Proxy
	{

		private static var _map:Object = {};

		private var _iteration:Array;

		public function Embed()
		{
		}

		protected function register(name:String, resource:Class):void
		{
			_map[name] = resource;
		}

		public function getResourceByName(name:String):Object
		{
			if (name in _map)
			{
				var c:Class = _map[name];
				return new c();
			}

			return null;
		}

		flash_proxy override function deleteProperty(name:*):Boolean
		{
			return false;
		}

		flash_proxy override function getProperty(name:*):*
		{
			if (!(name in _map))
			{
				return undefined;
			}

			return new _map[name];
		}

		flash_proxy override function hasProperty(name:*):Boolean
		{
			return name in _map;
		}

		flash_proxy override function nextName(index:int):String
		{
			return _iteration[index - 1].name;
		}

		flash_proxy override function nextNameIndex(index:int):int
		{
			if (index == 0)
			{
				_iteration = [];
				for (var n:String in _map)
				{
					_iteration.push({name:n, value:_map[n]});
				}
			}

			if (index < _iteration.length)
			{
				return index + 1;
			}
			else
			{
				_iteration = null;
				return 0;
			}
		}

		flash_proxy override function nextValue(index:int):*
		{
			return new _iteration[index - 1].value;
		}
	}
}